<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="translate(title)"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item :label="translate('按钮编码') + '：'" prop="code">
        <el-select v-model="form.code">
          <el-option
            v-for="item in codeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('按钮名称') + '：'" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item :label="translate('上级按钮') + '：'" prop="parentId">
        <el-tree-select
          v-model="form.parentId"
          :check-strictly="true"
          :data="tree"
          node-key="ButtonId"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
        />
      </el-form-item>
      <el-form-item :label="translate('分类') + '：'">
        <el-select v-model="form.category" :disabled="true">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="translate('icon图标') + '：'" prop="icon">
        <el-input v-model="form.icon" />
      </el-form-item>
      <el-form-item :label="translate('js事件') + '：'" prop="jsEvent">
        <el-input v-model="form.jsEvent" />
      </el-form-item>
      <el-form-item :label="translate('Action地址') + '：'" prop="actionEvent">
        <el-input v-model="form.actionEvent" />
      </el-form-item>
      <el-form-item :label="translate('显示顺序') + '：'" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item :label="translate('选项') + '：'">
        <el-checkbox
          v-model="form.enabled"
          :checked="form.enabled === 1 ? true : false"
          :false-label="0"
          :true-label="1"
        >
          {{ translate('有效') }}
        </el-checkbox>
        <!-- <el-checkbox-group v-model="form.checkList">
          <el-checkbox label="a">分开线</el-checkbox>
          <el-checkbox label="b">有效</el-checkbox>
        </el-checkbox-group> -->
      </el-form-item>
      <el-form-item :label="translate('说明') + '：'">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="Please input"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">{{ translate('取消') }}</el-button>
      <el-button type="primary" @click="save">
        {{ translate('确定') }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { doEdit, getbuttoninfo } from '@/api/buttonManagement'
  import { translate } from '@/i18n'
  export default defineComponent({
    name: 'MenuManagementEdit',
    // components: { VabIconSelector },
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    emits: ['fetchData', 'data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        form: {
          meta: {},
          category: '1',
          enabled: 0,
        },
        defaultProps: {
          children: 'ChildNodes',
          label: 'ButtonName',
        },
        options: [
          {
            value: '1',
            label: translate('工具栏'),
          },
        ],
        codeList: [
          {
            value: 'add',
            label: 'add',
          },
          {
            value: 'edit',
            label: 'edit',
          },
          {
            value: 'delete',
            label: 'delete',
          },
          {
            value: 'detail',
            label: 'detail',
          },
          {
            value: 'Authentication',
            label: 'Authentication',
          },
          {
            value: 'RoleUser',
            label: 'RoleUser',
          },
          {
            value: 'RoleUserBatch',
            label: 'RoleUserBatch',
          },
          {
            value: 'RolePermission',
            label: 'RolePermission',
          },
          {
            value: 'RoleScopePermission',
            label: 'RoleScopePermission',
          },
          {
            value: 'replace',
            label: 'replace',
          },
        ],
        rules: {
          code: [
            {
              required: true,
              trigger: 'change',
              message: translate('请选择按钮编码'),
            },
          ],
          fullName: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入按钮名称'),
            },
          ],
          // parentId: [
          //   { required: true, trigger: 'blur', message: '请输入上级按钮' },
          // ],
          category: [
            {
              required: true,
              trigger: 'change',
              message: translate('请选择模块分类'),
            },
          ],
          icon: [
            {
              required: false,
              trigger: 'blur',
              message: translate('请输入icon名称'),
            },
          ],
          ActionEvent: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入Action地址'),
            },
          ],
          sortCode: [
            {
              required: false,
              trigger: 'change',
              message: translate('请输入显示顺序'),
            },
          ],
          remark: [
            {
              required: true,
              trigger: 'change',
              message: translate('请输入说明'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })
      const handleIcon = (item) => {
        state.form.meta.icon = item
      }
      //获取详情数据
      const getmenu = async (row) => {
        state.listLoading = true
        const {
          data: { data },
        } = await getbuttoninfo(row)
        state.form = data
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, param) => {
        if (!row.ButtonId) {
          state.title = '添加'
          state.form.moduleId = row
        } else {
          state.title = '编辑'
          getmenu({ buttonId: row.ButtonId })
        }
        state.params = JSON.parse(JSON.stringify(param))
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {},
          category: '1',
          enabled: 0,
        }
        emit('data', [])
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            if (!state.form.parentId) state.form.parentId = 0
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetchData', state.params)
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        handleIcon,
        showEdit,
        close,
        save,
        translate,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 234px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
